package com.sanyang.pda.ui.replace;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.adapter.SampleBackAdapter;
import com.sanyang.pda.bean.HaveTestedBean;
import com.sanyang.pda.bean.request.SampleReturnRequest;
import com.sanyang.pda.contract.SampleBackContract;
import com.sanyang.pda.dialog.ConfrimDialog;
import com.sanyang.pda.dialog.SampleBackDialog;
import com.sanyang.pda.presenter.SampleBackPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.SAMPLEBACKACTIVITY)
public class SampleBackActivity extends BaseMvpActivity<SampleBackPresenter> implements SampleBackContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    SampleBackAdapter adapter;
    @BindView(R.id.tv_return)
    TextView tvReturn;
    @BindView(R.id.cb_all)
    CheckBox cbAll;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv_no_return)
    TextView tvNoReturn;
    private String entryNo;

    @Override
    public SampleBackPresenter getPresenter() {
        return new SampleBackPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_sampleback;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SampleBackAdapter(R.layout.item_sample_back, new ArrayList<>());
        rvList.setAdapter(adapter);
    }

    @Override
    public void initLinsenterner() {
        cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (HaveTestedBean item : adapter.getData()) {
                    item.setSelect(isChecked);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void getData() {
        Bundle bundle = getIntent().getExtras();
        entryNo = bundle.getString(StringConfig.ENTRYNO);
        tv1.setText(entryNo);
        presenter.samplingResultList(entryNo);
    }

    private List<SampleReturnRequest> getList(String pallet, String location) {
        List<SampleReturnRequest> sampleReturnRequests = new ArrayList<>();
        for (HaveTestedBean haveTestedBean : adapter.getData()) {
            if (haveTestedBean.isSelect()) {
                SampleReturnRequest sampleReturnRequest = new SampleReturnRequest();
                sampleReturnRequest.setArticleId(haveTestedBean.getArticleId());
                sampleReturnRequest.setCheckNum(haveTestedBean.getCheckNum() + "");
                sampleReturnRequest.setEntryNo(entryNo);
                sampleReturnRequest.setBackPallet(pallet);
                sampleReturnRequest.setSamplingResultNo(haveTestedBean.getSamplingResultNo());
                sampleReturnRequest.setSamplingNo(haveTestedBean.getSamplingNo());
                sampleReturnRequest.setBackStoragebinId(location);
                sampleReturnRequest.setFromPallet(haveTestedBean.getFromPallet());
                sampleReturnRequests.add(sampleReturnRequest);
            }
        }
        return sampleReturnRequests;
    }
    private List<String> getNoList() {
        List<String> sampleReturnRequests = new ArrayList<>();
        for (HaveTestedBean haveTestedBean : adapter.getData()) {
            if (haveTestedBean.isSelect()) {
                sampleReturnRequests.add(haveTestedBean.getSamplingResultNo());
            }
        }
        return sampleReturnRequests;
    }
    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finishActivity();
    }

    @OnClick(R.id.tv_return)
    public void onViewClicked1() {
        if (getList("", "").size() <= 0) {
            showMessage(getString(R.string.please_choose));
            return;
        }
        SampleBackDialog sampleBackDialog = new SampleBackDialog(mContext, new SampleBackDialog.ConfirmClick() {
            @Override
            public void onConfirmClick(String pallet, String location) {
                presenter.sampleReturn(getList(pallet, location));
            }
        });
        sampleBackDialog.show();
    }

    @Override
    public void setList(List<HaveTestedBean> list) {
        adapter.setNewInstance(list);
    }

    @Override
    public void onSuccess() {
        presenter.samplingResultList(entryNo);
    }



    @OnClick(R.id.tv_no_return)
    public void onViewClicked3() {
        if (getNoList().size() <= 0) {
            showMessage(getString(R.string.please_choose));
            return;
        }
        ConfrimDialog confrimDialog = new ConfrimDialog(mContext, new ConfrimDialog.Onclick() {
            @Override
            public void onConfirm() {
                presenter.sampleIsNotBack(getNoList());
            }
        });
        confrimDialog.show(getString(R.string.querennoreturn));
    }
}
